package com.itstherules.jmsunit.expectation.xml;

import org.junit.Test;

import com.itstherules.jmsunit.expectation.xml.xpath.XPathEqualsExpectation;

public class XPathExpectationTest {

	private static String XML = "<root>" + "<shoot>" + "<abba/>"
			+ "<acdc></acdc>" + "<eno>eno</eno>" + "</shoot>" + "<boot>"
			+ "<UNKLE>UNKLEMix</UNKLE>" + "<lfo>lowFrequencyOscillation</lfo>"
			+ "<soundgarden>superunknown</soundgarden>" + "</boot>" + "</root>";

	@Test
	public void xPathEqualsFullXmlAllPresentAndMore() throws Exception {
		XPathEqualsExpectation unit = new XPathEqualsExpectation("eno",
				"/root/shoot/eno");
		unit.compute(XML);
		unit = new XPathEqualsExpectation("UNKLEMix", "/root/boot/UNKLE");
		unit.compute(XML);
		unit = new XPathEqualsExpectation("superunknown",
				"/root/boot/soundgarden");
		unit.compute(XML);
	}

	@Test(expected = IllegalArgumentException.class)
	public void xPathEqualsWithInvalidXpath() throws Exception {
		XPathEqualsExpectation unit = new XPathEqualsExpectation("",
				"im_a/lovely!@bunch_of_nonsense\\d");
		unit.compute(XML);
	}

	@Test(expected = IllegalArgumentException.class)
	public void xPathEqualsFullXmlXPathNotFoundAndExpectedNull()
			throws Exception {
		XPathEqualsExpectation unit = new XPathEqualsExpectation(null,
				"/fish/chips");
		unit.compute(XML);
	}

	@Test(expected = AssertionError.class)
	public void xPathEqualsFullXmlXPathNotFound() throws Exception {
		XPathEqualsExpectation unit = new XPathEqualsExpectation("amalfis",
				"/fish/chips");
		unit.compute(XML);
	}

	@Test(expected = IllegalArgumentException.class)
	public void xPathEqualsFullXmlXPathNoChildAndNull() throws Exception {
		XPathEqualsExpectation unit = new XPathEqualsExpectation(null,
				"/root/shoot/fruit");
		unit.compute(XML);
	}

	@Test
	public void xPathEqualsFullXmlEmptyValueClosedTag() throws Exception {
		XPathEqualsExpectation unit = new XPathEqualsExpectation("",
				"/root/shoot/abba");
		unit.compute(XML);
	}

	@Test
	public void xPathEqualsFullXmlEmptyValue() throws Exception {
		XPathEqualsExpectation unit = new XPathEqualsExpectation("",
				"/root/shoot/acdc");
		unit.compute(XML);
	}

	@Test
	public void xPathEqualsAllPresent() throws Exception {
		String xml = 				
			"<root>" +
				"<shoot>" +
					"<eno>eno</eno>" +
				"</shoot>" +
				"<boot>" +
					"<UNKLE>UNKLEMix</UNKLE>" +
					"<soundgarden>soundgardenmessage</soundgarden>" +
				"</boot>" +
			"</root>";
		XPathEqualsExpectation unit = new XPathEqualsExpectation("eno", "/root/shoot/eno");
		unit.compute(xml); 
		unit = new XPathEqualsExpectation("UNKLEMix", "/root/boot/UNKLE");
		unit.compute(xml); 
		unit = new XPathEqualsExpectation("soundgardenmessage", "/root/boot/soundgarden");
		unit.compute(xml); 
	}

}
